package com.bjp.seekersprofile.action;

import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bjp.seekersprofile.model.Profile;
import com.bjp.seekersprofile.service.ProfileLocalServiceUtil;
import com.bjp.seekersprofile.service.ProfileServiceUtil;
import com.bjp.seekersprofile.service.persistence.ProfileUtil;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.struts.PortletAction;


public class ManageAction extends PortletAction {
	
	public ActionForward render(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res) throws Exception {
		
		String forward = "";
		String cmd = ParamUtil.getString(req, Constants.CMD);
		
		if (cmd.equals(Constants.EDIT)) {
			forward = update(mapping, form, config, req, res);
		} else if (cmd.equals(Constants.ADD)) {
			forward = "bjp.seekersprofile.add";
		} else if (cmd.equals(Constants.VIEW)) {
			forward = view(mapping, form, config, req, res);
		} else if (cmd.equals(Constants.MANAGE)) {
			forward = editExtras(mapping, form, config, req, res);
		} else if (cmd.equals(Constants.PREVIEW)) {
			forward = interaction(mapping, form, config, req, res);
		} else {
			//
		}
		
		return mapping.findForward(forward);
	}
	
	/*
	 * For Loading from manage.
	 */
	private String view(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {
		
		long profileId = ParamUtil.getLong(req, "profileId");
		
		Profile profile = ProfileLocalServiceUtil.getProfile(profileId);
		
		req.setAttribute("PROFILE", profile);
		
		return "bjp.seekersprofile.view";
		
	}
	
	/**
	 * 
	 */
	private String update(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {
		
		long profileId = ParamUtil.getLong(req, "profileId");
		
		Profile profile = ProfileLocalServiceUtil.getProfile(profileId);
		
		req.setAttribute("PROFILE", profile);
		
		return "bjp.seekersprofile.add";
		
	}
	/*
	 * For Updating the extra details of the profile.
	 */
	private String editExtras(ActionMapping mapping, ActionForm form, 
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {
		
		String tabs = ParamUtil.getString(req, "tabs1");
		long profileId = ParamUtil.getLong(req, "profileId");
		
		Profile profile = ProfileLocalServiceUtil.getProfile(profileId);
		
		req.setAttribute("PROFILE", profile);
		
		return "bjp.seekersprofile.add_qualification";
	}
	private String interaction(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {
		
		long profileId = ParamUtil.getLong(req, "profileId");
		
		Profile profile = ProfileLocalServiceUtil.getProfile(profileId);
		
		req.setAttribute("PROFILE", profile);
		
		return "bjp.seekersprofile.interaction";
		
	}
}
